#ifndef CHANNEL_H
#define CHANNEL_H

#include <functional>
class EventLoop;

class Channel{
public:
    using EventCallback = std::function<void()>;

    Channel(int fd) : fd_(fd), events_(0), revents_(0) {}
    Channel(EventLoop* loop, int fd);
    ~Channel();

    void handleEvent();
    void setReadCallback(EventCallback cb);
    void setWriteCallback(EventCallback cb);
    void setCloseCallback(EventCallback cb);
    void setErrorCallback(EventCallback cb);

    int fd() const {
        return fd_;
    }

    int events() const {
        return events_;
    }

    int revents() const { 
        return revents_; 
    }

    void set_revents(int revt){
        revents_ = revt;
    }

    void set_events(int evt){
        events_ = evt;
    }

    void enableReading();
    void disableReading();
    void enableWrite();
    void disableWrite();
    void disableAll();
    void remove();

private:
    void update();

    EventLoop* loop_;
    int fd_;
    int events_;
    int revents_;

    EventCallback readCallback_;
    EventCallback writeCallback_;
    EventCallback closeCallback_;
    EventCallback errorCallback_;
};

#endif